package com.stalltea.spider;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author yivlchangfa
 * @since 2021/9/23 19:35
 */
public class Spider {

    public static final String TITLE_CLASS_NAME = "text-gray";

    public static final String GROUP_ROW_CLASS_NAME = "row";

    public static void main(String[] args) throws IOException {
        String url = "https://jubt.live/cn/index.html";
        HttpGet get = new HttpGet(url);
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpResponse response = httpClient.execute(get);
        response.getEntity();
        String content = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        Document document = Jsoup.parse(content);
        Element elementContent = document.select(".main-content").get(0);
        Elements elements = elementContent.children();
        Map<String, SiteGroup> siteGroupMap = Maps.newHashMap();
        String nowGroupName = "";
        for (Element element : elements) {
            String name = element.attr("class");

            if (TITLE_CLASS_NAME.equals(name)) {
                String groupName = element.text().trim();
                nowGroupName = groupName;
                continue;
            }

            if (GROUP_ROW_CLASS_NAME.equals(name)) {
                //获取网站信息
                Elements siteElements = element.children();
                List<Site> siteList = Lists.newArrayList();
                for (Element siteElement : siteElements) {
                    Site site = new Site();
                    Optional<Element> siteUrlElement = findFirstElement(siteElement.select("div .xe-widget "));
                    Optional<Element> siteNameElement = findFirstElement(siteElement.select(".xe-comment strong"));
                    Optional<Element> siteMarkElement = findFirstElement(siteElement.select(".overflowClip_2"));
                    siteUrlElement.ifPresent(e -> site.setUrl(e.attr("data-original-title").trim()));
                    siteNameElement.ifPresent(e -> {
                        String[] tmpArray = e.text().split("\\|");
                        if (tmpArray.length > 0) {
                            site.setName(tmpArray[0].trim());
                        }
                    });
                    siteMarkElement.ifPresent(e -> site.setMark(e.text()));
                    siteList.add(site);
                }
                SiteGroup siteGroup = siteGroupCreate(siteGroupMap, nowGroupName);
                siteGroup.getSiteList().addAll(siteList);
            }
        }
        String json = JSON.toJSONString(siteGroupMap);
        File file = new File("F:/url.txt");
        OutputStream os = new FileOutputStream(file);
        os.write(json.getBytes(StandardCharsets.UTF_8));
    }

    private static SiteGroup siteGroupCreate(Map<String, SiteGroup> siteGroupMap, String groupName) {
        SiteGroup group = null == siteGroupMap.get(groupName)
                ? new SiteGroup(groupName, Lists.newArrayList()) : siteGroupMap.get(groupName);
        siteGroupMap.put(groupName, group);
        return group;
    }

    private static Optional<Element> findFirstElement(Elements elements) {
        if (null == elements) {
            return Optional.empty();
        }
        return Optional.of(elements.first());
    }

}
